package com.nttuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.nttuan.flows.ComingListener;
import com.nttuan.flows.PageChange;
import com.nttuan.model.entity.Task;
import com.nttuan.mycalender.R;
import com.nttuan.util.EmailParser;
import com.nttuan.util.Support;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by NT Tuan on 10/27/2014.
 */
public class ComingAdapter extends BaseAdapter{
    private List<Task> list;
    private Context context;
    private LayoutInflater inflater;
    private ComingListener listener;
    private String[] arrDayOfWeek;
    private int currentDay =2;

    private Date currentDate = Calendar.getInstance().getTime();

    public ComingAdapter(Context context, List<Task> list, String viewBy){
        this.list = list;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }
    public void setList(List<Task> list) {
        this.list = list;
    }
    public void setListener(ComingListener listener) {
        this.listener = listener;
    }
    public List<Task> getList() {
        return list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoller holler;
        if(view == null){
            holler = new ViewHoller();
            view = inflater.inflate(R.layout.item_coming_task, null);

            holler.status = view.findViewById(R.id.viewComingStatus);
            holler.taskName =(TextView)view.findViewById(R.id.textComingName);
            holler.timeStart =(TextView)view.findViewById(R.id.textComingTime);
            holler.timeLeft =(TextView)view.findViewById(R.id.textTimeLeftComing);
            holler.alarm =(ImageView)view.findViewById(R.id.imageComingAlarm);
            holler.delete =(ImageView)view.findViewById(R.id.btnComingDel);
            holler.divider =(ImageView)view.findViewById(R.id.imgDividerComing);
            holler.repeat =(TextView)view.findViewById(R.id.textComingRepeat);
            holler.timeLeftLayout =(LinearLayout)view.findViewById(R.id.lLayoutComingTimeLeft);
            holler.repeatLayout =(LinearLayout)view.findViewById(R.id.lLayoutComingRepeat);
            holler.layoutDivider =(RelativeLayout)view.findViewById(R.id.rLayoutDividerComing);
            holler.bg =(RelativeLayout)view.findViewById(R.id.rLayoutComing);
            view.setTag(holler);
        }else
            holler =(ViewHoller)view.getTag();

        final Task comingTask = list.get(i);
        Date startAt = startAt(comingTask);
        int timeLeft = Support.timeLeft2(startAt);
        Date endDate = Support.addDateTime(comingTask.getStart(),comingTask.getDuring());
        String sTime = context.getString(R.string.coming_start_date, Support.displayTime(comingTask.getStart()),Support.displayTime(endDate));
        int temp = currentDay;
        holler.taskName.setText(comingTask.getTaskName());
        holler.timeStart.setText(sTime);

//        view by day

            holler.timeLeft.setText(null);
            if(i == 0)
                holler.divider.setVisibility(View.GONE);
            else
                holler.divider.setVisibility(View.VISIBLE);



        if(timeLeft < 0 ) {
            holler.timeLeftLayout.setVisibility(View.VISIBLE);
            holler.bg.setBackgroundResource(R.drawable.coming_second_bg);
        }else{
            if (timeLeft  > comingTask.getDuring()) {
                holler.bg.setBackgroundResource(R.drawable.coming_second_bg);
                holler.timeLeftLayout.setVisibility(View.VISIBLE);
            }else {
                holler.timeLeftLayout.setVisibility(View.GONE);
                holler.bg.setBackgroundResource(R.drawable.coming_primary_bg);
            }
        }


        if(comingTask.getRemind() == 0)
            holler.alarm.setImageResource(R.drawable.ic_alarm_off_grey600_24dp);
        else
            holler.alarm.setImageResource(R.drawable.ic_alarm_on_grey600_24dp);

        if(comingTask.getRepeat() != null && !comingTask.getRepeat().equals("") ){
            holler.repeatLayout.setVisibility(View.VISIBLE);
            holler.repeat.setText(EmailParser.displayRepeat(context, comingTask.getRepeat()));
        }else{
            holler.repeatLayout.setVisibility(View.GONE);
        }

        holler.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null)
                    listener.delete(comingTask.getTaskId());
            }
        });
        holler.alarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null)
                    listener.changeRemain(comingTask.getTaskId());
            }
        });
        return view;
    }
    private Date startAt(Task task){

        if(task.getRepeat() != null && !task.getRepeat().equals("")){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(task.getStart());
            int dayStart = calendar.get(Calendar.DAY_OF_WEEK);

            String[] arr = task.getRepeat().split(",");
            for(String day : arr){
                int temp = Integer.parseInt(day);
                if(dayStart == 7)
                    dayStart = 1;
                if(temp >= dayStart-1){
                    calendar.set(Calendar.DAY_OF_WEEK, temp+1);
                    return  calendar.getTime();
                }
            }
        }
        else
            return task.getStart();
        return null;
    }
    class ViewHoller{
        TextView taskName;
        TextView timeStart;
        TextView repeat;
        TextView timeLeft;
        LinearLayout timeLeftLayout;
        LinearLayout repeatLayout;
        RelativeLayout bg;
        RelativeLayout layoutDivider;

        View status;
        ImageView alarm;
        ImageView delete;
        ImageView divider;
    }
}
